<?php

namespace app\controllers;

use yii\web\Controller;
use app\models\InsuranceProject;
use app\models\InsurancePlan;
use app\models\InsuranceProvider;
use app\models\InsurancePrice;
use app\models\InsuranceBenifit;
use app\models\InsuranceBenifitGroup;

class InsuranceController extends Controller {
	
	
	public $userinfo = array (
			'id' => 'o5HMHwc0PtR_hDtQ0u4BpK8NkZeE',
			'nickname' => 'Bing大人',
			'avatar' => 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLCTTQPUyhVsR2WTcvSaVmKXjDnkQYOMXicQribBYHKO1v6H9hAzZADagTdDYHP9CnkfGz0QXjTJtQCg/0' 
	);
	public function actionIndex() {
		$user = $this->userinfo;
		
		$project = InsuranceProject::find ()->where ( [ 
				'status' => 'ok' 
		] )->orderBy ( 'project_order ASC' )->all ();
		
		return $this->render ( 'home', [ 
				'project' => $project 
		] );
	}
	public function actionProject() {
		$user = $this->userinfo;
		$project_code = \Yii::$app->request->get ( 'project_code' );
		
		$project = InsuranceProject::find ()->where ( [ 
				'project_code' => $project_code 
		] )->orderBy ( 'project_order DESC' )->one ();
		
		if (empty ( $project )) {
			header ( 'location:' . '/insurance/' );
		}
		$plan = InsurancePlan::find ()->where ( [ 
				'project_code' => $project_code 
		] )->orderBy ( 'plan_order DESC' )->all ();
		
		$benifit_group = InsuranceBenifitGroup::find ()->where ( [ 
				'project_code' => $project_code 
		] )->orderBy ( 'order DESC' )->all ();
		
		$benifit = array ();
		foreach ( $plan as $b ) {
			$benifit [$b->plan_code] = InsuranceBenifit::find ()->where ( [ 
					'plan_code' => $b->plan_code 
			] )->all ();
		}
		
		$price = InsurancePrice::find ()->where ( [ 
				'project_code' => $project_code 
		] )->all ();
		
		return $this->render ( 'project', [ 
				'project' => $project,
				'plan' => $plan,
				'price' => $price,
				'benifit_group' => $benifit_group,
				'benifit' => $benifit 
		] );
	}
}